﻿var currentPage = 1; // 当前页
var pageSize = 18; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数

$(document).ready(function() {
    showList(currentPage);
});

function search() {
    showList(currentPage);
}
// 条件查询资源列表
function UpdateHousing() {
    var userid_from = document.getElementById("slusername").value; //转出人
    var deptid_from = document.getElementById("ddlDept1").value;
    var username_from = LexunJS.GetSelectText("slusername");
    var deptname_from = LexunJS.GetSelectText("ddlDept1");
    if (username_from == "请选择") {
        alert("请选择转出人！");
        return;
    }


    var userid_to = document.getElementById("slusername1").value; //转入人
    var username_to = LexunJS.GetSelectText("slusername1");
    var deptid_to = document.getElementById("ddlDept2").value;
    var deptname_to = LexunJS.GetSelectText("ddlDept2");
    if (username_to == "请选择") {
        alert("请选择转入人！");
        return;
    }

    var id = document.getElementById("hID").value;
    var flag = document.getElementById("Radio2").checked;

    if (flag == false) {
        if (id == "") {
            alert("请选择客源！");
            return;
        }
    }
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=UpdateStatus&id=' + id + '&deptid_from=' + deptid_from + '&userid_from=' + userid_from + '&username_from=' + username_from + '&deptname_from=' + deptname_from + '&flag=' + flag + '&userid_to=' + userid_to + '&deptid_to=' + deptid_to + '&deptname_to=' + deptname_to + '&username_to=' + username_to,
        timeout: '10000',
        error: function() {
            alert("系统繁忙,请稍候再试！");
        }, success: function(json) {
            alert(json);
            document.getElementById("hID").value = "";
            showList(currentPage);
        }
    });
}
// 条件查询资源列表
function showList(currentPage) {
    var userid = document.getElementById("slusername").value;
    var id = document.getElementById("customerid").value;
    var status = document.getElementById("slstatus").value;
    var isself = document.getElementById("slIsSelf").value;
    if (status == "全部") {
        status = "-1";
    }
   
    $("#divPage").html(""); // 清空
    $("#divList").html("<img src=\"../images/loading.gif\" />"); // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerCustomer.ashx',
        data: 'act=p_CustomerList&page=' + currentPage + '&pagesize=' + pageSize + '&userid=' + userid + '&cid=' + id + '&status=' + status + '&isself=' + isself,
        timeout: '10000',
        error: function() {
            alert("系统繁忙,请稍候再试！");
        }, success: function(json) {
            listCallBack(json);
        }
    });
}
/*
功能: 将返回的json数据进行排版
json: json数据        
*/
function listCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var page = json.page;
    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;
    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("序号");
    arr.push("</td>"); arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("客户");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("状态");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("私客");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("城区");
    arr.push("</td>"); arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("片区");
    arr.push("</td>"); arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("用途");
    arr.push("</td>"); arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("交易");
    arr.push("</td>"); 
    arr.push("</tr>");

    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' ondblclick=\"ReturnValue('" + record[i].id + "','" + record[i].buildname + "')\" onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' ondblclick=\"ReturnValue('" + record[i].id + "','" + record[i].buildname + "')\" onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='20' style='text-align: left;' class='STYLE6'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].id);
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].customer + "</a>");

        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].status == 0) {
            arr.push("当前");
        } else {
            arr.push("资料");
        }
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].isself == 0) {
            arr.push("私客");
        } else {
            arr.push("公客");
        }
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].dname);
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].area);
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(LexunJS.GetUsageNameById(record[i].usage));
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].tradetype);
        arr.push("</td>");



        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表

    $("#divPage").html(""); // 清空

    if (pageall > 1) {
        var pageHtml = LexunJS.showPage2(currentPage, pageall, "showList"); // 分页
        $("#divPage").html(pageHtml);
    }
}
// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    return false;
}
// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        LexunJS.addOrCutData(1, "hID", objvalue);

    }
    else {
        LexunJS.addOrCutData(0, "hID", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("hID").value = "";

    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox" && checkList[i].id > 0) {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "hID", checkList[i].id);
            }
        }
    }
    else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox" && checkList[i].id > 0) {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "hID", checkList[i].id);
            }
        }
    }
}
//范围
function selectValue() {
    var flag = document.getElementById("Radio1").checked;
    if (flag == true) {
        document.getElementById("customerid").disabled = false;
        document.getElementById("slstatus").disabled = false;
        document.getElementById("btnSearch").disabled = false;
        document.getElementById("slIsSelf").disabled = false;
        showList(currentPage);
    } else {
        document.getElementById("customerid").disabled = true;
        document.getElementById("slstatus").disabled = true;
        document.getElementById("btnSearch").disabled = true;
        document.getElementById("slIsSelf").disabled = true;
        $("#divList").html(""); // 显示列表

        $("#divPage").html(""); // 清空
    }
}
// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../CM/CustomerDetail.aspx?id=" + id + "&id_list=" + escape(id_list);

    self.parent.closeTab('客源详情');
    self.parent.addTab('客源详情', url, 'icon-nav');
    //window.open("CustomerDetail.aspx?id=" + id + "&id_list=" + escape(id_list), "target", LexunJS.openParams());
}